package com.lemon.smartsafetynew.ui.activity.ledger;

import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.android.http.HttpUtils;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.ImgLoadUtil;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafetynew.entity.contact.PersonVo;
import com.lemon.smartsafetynew.net.services.SafeLedgerServices;
import com.lemon.smartsafetynew.util.common.CallUtils;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.Bind;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Andrew on 2017/9/15.
 */

public class LedgerEmergencyActivity extends SwipeBackActivity {

    @Bind(R.id.user_icon_imageView)
    CircleImageView userIconImageView;
    @Bind(R.id.name_tv)
    TextView nameTv;
    @Bind(R.id.telephone_tv)
    TextView telephoneTv;
    @Bind(R.id.telephone_imageView)
    ImageView telephoneImageView;
    @Bind(R.id.department_tv)
    TextView departmentTv;
    @Bind(R.id.layout)
    LinearLayout layout;

    @Override
    protected void initTitle() {
        this.setBarTitle("应急处置");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(LedgerEmergencyActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_ledger_emergency;
    }

    @Override
    protected void initViews() {
        super.initViews();

        getNetData();
    }

    private void getNetData() {

        SafeLedgerServices
                .init()
                .getEmergencyUser(new LemonRequestParams(""));

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {

        if (KeyConstants.URL_GET_SAFE_LEDGER_EMERGENCY_USER.equals(event.getTag())) { //获取紧急联系人信息
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.show();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    initUserView((List<PersonVo>) event.getObj());

                } else if (Constants.Result.ERROR == event.getEventResult()) {

                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });
                }
            }
        }
    }

    /**
     * 显示用户信息
     */
    private void initUserView(final List<PersonVo> personVos) {

        if(personVos == null || personVos.isEmpty()) {
            return;
        }

        final PersonVo personVo = personVos.get(0);

        nameTv.setText(com.lemon.smartsafetynew.util.common.CommonUtil.avoidNullString(personVo.getUser_name()));
        telephoneTv.setText(com.lemon.smartsafetynew.util.common.CommonUtil.avoidNullString(personVo.getMobile()));
        departmentTv.setText(com.lemon.smartsafetynew.util.common.CommonUtil.avoidNullString(personVo.getDepartment_name()));

        if (!CommonUtil.strIsEmpty(personVo.getPhotoURL())) {
            try {
                ImgLoadUtil.setImgPath(HttpUtils.formatFilesUrl(personVo.getPhotoURL()), userIconImageView);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            userIconImageView.setBackgroundResource(R.mipmap.img_placeholder);
        }

        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (personVo.getMobile() != null) {
                    CallUtils.callTel(personVo.getMobile());
                } else {
                    mHUDUtil.showErrorTip("公司无紧急联系人电话", new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });
                }

            }
        });
    }
}
